package com.jack.controller;


import com.jack.pojo.Dept;
import com.jack.result.Result;
import com.jack.service.DeptService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * 部门管理控制器
 */
@Slf4j
@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;


    /**
     * 新增部门
     * @param dept
     * @return
     */
    @PostMapping
    public  Result addDept (@RequestBody Dept dept) {
        deptService.addDept(dept);
        return Result.success();
    }

    /**
     * 根据id删除部门
     * @param id
     * @return
     */
    @DeleteMapping
    public Result deleteDeptById(Integer id) {
        deptService.deleteDeptById(id);
        return Result.success();
    }

    /**
     * 更新部门信息
     * @param dept
     * @return
     */
    @PutMapping
    public Result updateDept (@RequestBody Dept dept) {
        deptService.updateDept(dept);
        return Result.success();
    }


    /**
     * 根据id查询部门信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result getDeptById (@PathVariable Integer id){
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    /**
     * 获取所有部门
     * @return
     */
    @GetMapping
    public Result getAllDepts() {
        List<Dept> depts = deptService.getAllDepts();
        return Result.success(depts);
    }
}
